<?php
namespace app\merchant\controller;

use think\Db;

class Revert extends Common
{
    //运费模板列表图
    public function index()
    {
        $page_number = 10;
        $where['revert_merchant_id'] = session('merchant.merchant_id');
        $where['revert_is_delete'] = '0';
        //获取快递模板的列表
        $data['revert_list'] = Db::table('revert')->where($where)->paginate($page_number);

        $data['page'] = $data['revert_list']->render();
        return view('revert',['data' => $data]);
    }

    //添加归还地址页面
    public function add(){
        return view('addrevert');
    }

    //添加归还地址数据
    public function do_add(){
        $data['revent_receiver_name'] = input('post.name');
        $data['revert_merchant_id'] = session('merchant.merchant_id');
        $data['revent_province'] = input('post.province');
        $data['revent_city'] = input('post.city');
        $data['revent_region'] = input('post.region');
        $data['revent_detailed_address'] = input('post.address');
        $data['revent_postcode'] = input('post.postcode');
        $data['revent_tel'] = input('post.tel');
        $data['revert_create_time'] = time();
        $data['revert_update_time'] = time();
        $data['revert_is_delete'] = '0';
        $ret = Db::table('revert')->insert($data);
        if(!$ret){
            return returnJson(0, '添加失败');

        }
        return returnJson(1, '添加成功');
    }

    //修改运费模板页面
    public function edit(){
        $revert_id = input('get.revert_id');
        $data['revert_list'] = Db::table('revert')->where('revert_id', 'eq', $revert_id)->find();
        return view('editrent',['data' => $data]);
    }

    //修改运费模板数据
    public function do_edit(){
        $revert_id = input('post.revert_id');
        $revert_list = Db::table('revert')->where('revert_id', 'eq', $revert_id)->find();
        if(!$revert_list){
            return returnJson('', 300, '该自提点不存在');
        }
        if($revert_list['revert_merchant_id'] != session('merchant.merchant_id')){//){
            return returnJson('', 300, '该自提点不属于你');
        }
        $where =[
            'revert_id' => $revert_id,
        ];
        $data['revent_receiver_name'] = input('post.name');
        $data['revent_province'] = input('post.province');
        $data['revent_city'] = input('post.city');
        $data['revent_region'] = input('post.region');
        $data['revent_detailed_address'] = input('post.address');
        $data['revent_postcode'] = input('post.postcode');
        $data['revent_tel'] = input('post.tel');
        $data['revert_update_time'] = time();
        $ret = Db::table('revert')->where($where)->update($data);
        if(!$ret){
            return returnJson(0,'修改自提点失败');
        }
        return returnJson(1, '修改自提点成功');
    }
    //删除商户快递模板
    public static function revert_del(){
        $revert_id = input('get.revert_id');
        $revert_list = Db::table('revert')->where('revert_id', 'eq', $revert_id)->find();
        if(!$revert_list){
            return returnJson('', 300, '该自提点不存在');
        }
        if($revert_list['revert_merchant_id'] != session('merchant.merchant_id')){
            return returnJson('', 300, '该自提点不属于你');
        }
        $where =[
            'revert_id' => $revert_id,
        ];
        $data['revert_is_delete'] = '1';
        $data['revert_update_time'] = time();
        $ret = Db::table('revert')->where($where)->update($data);
        return redirect('revert/index');
    }

    /**
     * 获取商家归还地址
     */
    public function getRevert()
    {
        $revert = Db::name('revert')
            ->where('revert_merchant_id', parent::$merchantId)
            ->where('revert_is_delete', 0)
            ->field('revert_id,revent_receiver_name,revent_province,revent_city,revent_region,revent_detailed_address')
            ->select();
        if (empty($revert)) {
            return returnJson(0, '', []);
        }
        $data = [];
        foreach ($revert as $item) {
            $data[] = [
                'revert_id' => $item['revert_id'],
                'revert_name' => $item['revent_receiver_name'],
                'revent_address' => $item['revent_province'] . $item['revent_city'] . $item['revent_region'] . $item['revent_detailed_address']
                ];
        }
        return returnJson(1, 'success', $data);
    }
}
